package com.wn.config;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * @author yellow Docter
 * @date 2022 -07-11
 * @desc
 *  启动 servlet的容器  (ioc容器 )
 */
public class ServletContainer extends AbstractDispatcherServletInitializer {

    // 创建javaweb容器
    @Override
    protected WebApplicationContext createServletApplicationContext() {

        //创建容器
        AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();

        //添加springmvc的配置
        annotationConfigWebApplicationContext.register(SpringMvcConfig.class);

        return annotationConfigWebApplicationContext;
    }

    //设置由springmvc控制器处理的请求映射路径
    // 拦截了所有的请求 交给 springmvc 进行请求处理
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //加载spring配置类
    @Override
    protected WebApplicationContext createRootApplicationContext() {

        return null;
    }

    /**
     * 配置过滤器操作
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {

        // springmvc提供的过滤器
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("utf-8");
        return new Filter[]{characterEncodingFilter};

    }
}
